import request from '@/utils/request'
import {
	parseStrEmpty
} from "@/utils/ruoyi";

/**
 * @param {Object} data
 * 首页统计数据
 */
export function getStatistics() {
	return request({
		url: '/device/device/statistics',
		method: 'get',
	})
}

//给当前用户绑定设备绑定设备
export function bind(data) {
	let prop = data.type + "Sn";
	let para = {}
	para[prop] = data.sn
	para["name"] = data.name
	para["address"] = data.address

	return request({
		url: '/device/' + data.type + '/bind',
		method: 'post',
		data: para
	})
}

//修改设备名称和地址
export function update(data) {
	let prop = data.type + "Sn";
	let para = {}
	para[prop] = data.sn
	para["name"] = data.name
	para["address"] = data.address

	return request({
		url: '/device/' + data.type + '/update',
		method: 'put',
		data: para
	})
}

//修改设备名称和地址
export function unbind(data) {
	let prop = data.type + "Sn";
	let para = {}
	para[prop] = data.sn

	return request({
		url: '/device/' + data.type + '/unbind',
		method: 'delete',
		data: para
	})
}

//type:设备类型
export function getList(data) {
	return request({
		url: '/device/' + data.type + '/list',
		method: 'get',
		params: {
			pageNum: data.pageNum,
			pageSize: data.pageSize,
			status: data.status
		},
	})
}

/**
 * 获取SN列表,
 * 向 device服务发请求
 */
export function getSns() {
	return request({
		url: '/device/device/sns',
		method: 'get',
	})
}

/**
 * @param {Object} data
 * 获取该用户拥有的设备的sn列表
 * 向 充电桩/UPS 等具体服务发请求
 */
export function getSnList(type) {
	return request({
		url: '/device/' + type + '/snList',
		method: 'get',
	})
}

/**
 * @param {Object} data
 * 获取历史数据
 * data = {
	 device: xxx, charger/ups/inverter
	 sn: xxx,
	 startDate: xxx,
	 endDate: xxx,
	 pageNum: 1,
	 pageSize: 10,
 }
 */
export function getHistory(data) {
	return request({
		url: '/device/' + data.type + '/history',
		method: 'post',
		data: data,
		// params: {
		// 	sn: data.sn,
		// 	startDate: data.startDate,
		// 	endDate: data.endDate,
		// 	pageNum: data.pageNum,
		// 	pageSize: data.pageSize,
		// },
	})
}

export function getData(sn) {
	let device = ""
	if (sn.indexOf("charger") != -1) {
		device = "charger"
	} else if (sn.indexOf("ups") != -1) {
		device = "ups"
	} else if (sn.indexOf("inverter") != -1) {
		device = "inverter"
	}
	return request({
		url: '/device/' + device + '/data',
		method: 'get',
		params: {
			sn: sn
		},
	})
}

/**
 * @param {Object} sn
 * 设置参数
 * data = {
	sn: xxx
	func_code: xxx
	value: xxx
	connection: xxx mqtt/socket
 }
 */
export function setParameter(data) {
	console.log("data=" + JSON.stringify(data));
	let device = ""
	if (data.sn.indexOf("charger") != -1) {
		device = "charger"
	} else if (data.sn.indexOf("ups") != -1) {
		device = "ups"
	} else if (data.sn.indexOf("inverter") != -1) {
		device = "inverter"
	}
	return request({
		url: '/device/' + device + '/parameter',
		method: 'post',
		data: data
	})
}

/**
 * @param {Object} data
 * 根据sn列表删除历史数据
 */
export function deleteHistory(data) {
	return request({
		url: '/device/' + data.type + '/history',
		method: 'put',
		data: {
			ids: data.ids
		}
	})
}